﻿using System;
using System.Data.SqlClient;
using System.IO;
using NUnit.Framework;

namespace Luna.Data.Test
{
    public class RepositoryTest : DataTest
    {
        public RepositoryTest(string providerType)
            : base(providerType)
        {
        }

        private const string NotFoundFile = "xxx.mdf";

        public IRepositoryManager Manager { get; set; }

        [SetUp]
        public void Configure()
        {
            foreach (var file in Directory.GetFiles(Environment.CurrentDirectory, "*.mdf"))
            {
                File.Delete(file);
            }

            foreach (var file in Directory.GetFiles(Environment.CurrentDirectory, "*.ldf"))
            {
                File.Delete(file);
            }

            Manager = Context.Get<IRepositoryManager>();
        }

        [TearDown]
        public void Clean()
        {
            Console.WriteLine("Cleaning " + Manager.Current.ConnectionString);
            Manager.Disconnect();
            Console.WriteLine("Cleaned");

            foreach (var file in Directory.GetFiles(Environment.CurrentDirectory, "*.mdf"))
            {
                File.Delete(file);
            }

            foreach (var file in Directory.GetFiles(Environment.CurrentDirectory, "*.ldf"))
            {
                File.Delete(file);
            }
        }

        [Test]
        public void TestCreate()
        {
            string fileName = "create.mdf";
            Manager.Create(fileName);
            Expect(File.Exists(fileName));
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(Manager.Current.ConnectionString);
            Expect(builder.AttachDBFilename == Path.GetFullPath(fileName));
        }

        [Test]
        public void TestOpen()
        {
            string fileName = "open.mdf";
            Manager.Create(fileName);
            Manager.Disconnect();
            Manager.Open(fileName);
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(Manager.Current.ConnectionString);
            Expect(builder.AttachDBFilename == Path.GetFullPath(fileName));
        }

        [Test]
        public void TestOpenFailed()
        {
            Expect(() => Manager.Open(NotFoundFile), Throws.Exception);
        }

        [Test]
        public void TestSaveAs()
        {
            string fileName = "save.mdf";
            string fileName2 = "save2.mdf";
            Manager.Create(fileName);
            Expect(Manager.Current.DataSourceDescription, Is.Not.Empty);
            Manager.SaveAs(fileName2);
            Expect(File.Exists(fileName2));
            SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(Manager.Current.ConnectionString);
            Expect(builder.AttachDBFilename == Path.GetFullPath(fileName2));
        }

        [Test]
        public void TestSaveFailed()
        {
            Expect(() => Manager.SaveAs(Path.GetTempFileName()), Throws.Exception);
        }
    }
}